<!-- 在项目配置页面中使用 -->
<template>
  <div class="project-config-page">
    
    <!-- 模板变量插入器 -->
    <template-variable-inserter
      :project-config="projectConfig"
      :current-cell="currentExcelCell"
      @insert-variable="onInsertVariable"
      @export-variables="onExportVariables"
    />

    <!-- Excel操作区域 -->
    <div class="excel-section">
      <el-card header="Excel模板操作">
        <el-input
          v-model="currentExcelCell"
          placeholder="当前Excel单元格（如：A1）"
          style="width: 200px; margin-right: 10px;"
        ></el-input>
        <el-button @click="openExcelFile">打开Excel文件</el-button>
        <el-button type="primary" @click="saveExcelFile">保存Excel</el-button>
      </el-card>
    </div>
  </div>
</template>

<script>
import TemplateVariableInserter from '@/components/TemplateVariableInserter'

export default {
  name: 'ProjectConfigPage',
  components: {
    TemplateVariableInserter
  },
  data() {
    return {
      projectConfig: {}, // 你的项目配置
      currentExcelCell: 'A1',
      excelApi: null
    }
  },
  methods: {
    onInsertVariable({ variable, code, cell }) {
        console.log('code:', code)
      console.log('插入变量:', variable, '到单元格:', cell || this.currentExcelCell)
      
      // 这里可以调用Excel API插入变量
      this.insertToExcel(variable, cell || this.currentExcelCell)
    },

    onExportVariables(data) {
      // 导出变量列表
      this.exportToFile(data, '参数对照表.json')
    },

    async insertToExcel(variable, cell) {
      try {
        // 调用后端API插入Excel
        const response = await this.$api.insertToExcel({
          filePath: this.excelFilePath,
          variable: variable,
          cell: cell
        })
        console.log('response:', response)
        this.$message.success('变量已插入到Excel')
      } catch (error) {
        this.$message.error('插入Excel失败: ' + error.message)
      }
    },

    exportToFile(data, filename) {
      const blob = new Blob([JSON.stringify(data, null, 2)], { 
        type: 'application/json' 
      })
      const url = URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.href = url
      link.download = filename
      link.click()
      URL.revokeObjectURL(url)
    },

    openExcelFile() {
      // 打开Excel文件的逻辑
    },

    saveExcelFile() {
      // 保存Excel文件的逻辑
    }
  }
}
</script>